#include "login.h"
#include "ui_login.h"

login::login(QWidget *parent) : QWidget(parent),
                                ui(new Ui::login)
{

    resize(QT_WIDTH, QT_HEIGHT);
    setStyleSheet("background-color: #FFFFFF;");

    QHBoxLayout *Hlayout = new QHBoxLayout();
    QSpacerItem *topSpace = new QSpacerItem(50, 130);
    Hlayout->addSpacerItem(topSpace);

    QLabel *label = new QLabel(this);
    label->setText("QT微信");
    label->setFont(QFont("Arial", 16, QFont::Bold));
    label->setAlignment(Qt::AlignCenter);
    label->setStyleSheet("color: #1dad28;");
    Hlayout->addWidget(label);

    QLineEdit *username = new QLineEdit(this);
    username->setFixedSize(300, 40);
    username->setPlaceholderText("请输入用户名");
    Hlayout->addWidget(username);

    QLineEdit *password = new QLineEdit(this);
    password->setEchoMode(QLineEdit::Password);
    password->setFixedSize(300, 40);
    password->setPlaceholderText("请输入密码");
    Hlayout->addWidget(password);

    QPushButton *loginButton = new QPushButton("登录", this);
    loginButton->setStyleSheet("QPushButton{background-color: #1dad28; color: #FFFFFF; border-radius: 5px;}"
                               "QPushButton:hover{background-color: #1dad28; color: #FFFFFF; border-radius: 5px;}"
                               "QPushButton:pressed{background-color: #1dad28; color:#FFFFFF; border-radius: 5px;}");
    loginButton->setFixedSize(300, 40);
    connect(loginButton, &QPushButton::clicked, [=]()
            {
        // 判断账号为admin密码为admin123
        if (username->text() =="admin"&& password->text()=="admin123")
        {
            qDebug() << "用户名：" << username->text() << "密码：" << password->text();
            QMessageBox::information(this, "登录成功", "用户名：" + username->text() + "\n密码：" + password->text());
            // 登录成功发送信号
            emit loginSuccess();
        }else{
            QMessageBox::warning(this, "错误", "请输入正确的用户名和密码");
            return;
        } });
    Hlayout->addWidget(loginButton);

    QSpacerItem *bottomSpace = new QSpacerItem(50, 180);
    Hlayout->addSpacerItem(bottomSpace);
    setLayout(Hlayout);
    QSpacerItem *leftSpace = new QSpacerItem(200, 50);
    QSpacerItem *rightSpace = new QSpacerItem(200, 50);
    QVBoxLayout *Vlayout = new QVBoxLayout();
    Vlayout->addSpacerItem(leftSpace);
    Vlayout->addItem(Hlayout);
    Vlayout->addSpacerItem(rightSpace);
    // setLayout(Vlayout);
}
login::~login()
{
    delete ui;
    if (usernameEdit)
        delete usernameEdit;
    if (passwordEdit)
        delete passwordEdit;
    if (loginButton)
        delete loginButton;
    if (passLabel)
        delete passLabel;
    if (loginButton)
        delete loginButton;
}
// 注册槽函数
void login::registerFn()
{
    qDebug() << "注册槽调用函数" << endl;
    emit toRegister();
    // QMessageBox::information(this, "注册", "注册功能开发中...");
}